package com.goods.business.service.imp;

import com.goods.business.mapper.BizSupplierMapper;
import com.goods.business.service.BizSupplierService;
import com.goods.common.model.business.BizSupplier;
import com.goods.common.utils.ListPageUtils;
import com.goods.common.vo.business.SupplierVO;
import com.goods.common.vo.system.PageVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class BizSupplierServiceImpl implements BizSupplierService {

    @Autowired
    private BizSupplierMapper bizSupplierMapper;

    @Override
    public PageVO<SupplierVO> findSupplierListByMap(Map<String, Object> paramMap) {
        String name = (String) paramMap.get("name");
        String address = (String) paramMap.get("address");
        String contact = (String) paramMap.get("contact");
        Example example = new Example(BizSupplier.class);
        /*添加条件*/
        if (name != null && !name.equals(""))
            example.createCriteria().andEqualTo("name", name);
        if (address != null && !address.equals(""))
            example.createCriteria().andEqualTo("address", address);
        if (contact != null && !contact.equals(""))
            example.createCriteria().andEqualTo("contact", contact);
        List<BizSupplier> bizSuppliers = bizSupplierMapper.selectByExample(example);

        List<SupplierVO> supplierVOS = new ArrayList<>();
        for (BizSupplier bizSupplier : bizSuppliers) {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(bizSupplier, supplierVO);
            supplierVOS.add(supplierVO);
        }
        List<SupplierVO> page = null;
        if (paramMap.get("pageNum") !=null && paramMap.get("pageSize") != null){
            page = ListPageUtils.page(supplierVOS, Integer.parseInt((String) paramMap.get("pageSize")), Integer.parseInt((String) paramMap.get("pageNum")));
            return new PageVO<>(bizSuppliers.size(),page);
        }
        return new PageVO<>(bizSuppliers.size(),supplierVOS);
    }

    @Override
    public void addSupplier(SupplierVO supplierVO) {
        BizSupplier bizSupplier = new BizSupplier();
        BeanUtils.copyProperties(supplierVO, bizSupplier);
        bizSupplierMapper.insert(bizSupplier);
    }

    @Override
    public SupplierVO getSupplierVo(Integer id) {
        BizSupplier bizSupplier = bizSupplierMapper.selectByPrimaryKey(id);
        SupplierVO supplierVO = new SupplierVO();
        BeanUtils.copyProperties(bizSupplier, supplierVO);
        return supplierVO;
    }

    @Override
    public void updateBizSupplier(Integer id, SupplierVO supplierVO) {
        BizSupplier bizSupplier = bizSupplierMapper.selectByPrimaryKey(id);
        if (bizSupplier!=null) {
            BeanUtils.copyProperties(supplierVO, bizSupplier);
            bizSupplier.setId(bizSupplier.getId());
            bizSupplierMapper.updateByPrimaryKey(bizSupplier);
        }
    }

    @Override
    public void deleteSupplierById(Integer id) {
        bizSupplierMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<SupplierVO> findAll() {
        List<BizSupplier> bizSuppliers = bizSupplierMapper.selectByExample(new Example(BizSupplier.class));
        ArrayList<SupplierVO> supplierVOS = new ArrayList<>();
        for (BizSupplier bizSupplier : bizSuppliers) {
            SupplierVO supplierVO = new SupplierVO();
            BeanUtils.copyProperties(bizSupplier, supplierVO);
            supplierVOS.add(supplierVO);
        }
        return supplierVOS;
    }
}
